<!-- components/UserDynamicList.vue -->
<template>
  <div class="dynamic-list">
    <div
      class="dynamic-card"
      v-for="item in list"
      :key="item.dynamicId"
      @click="() => !inPreviewMode && goDetail(item.dynamicId)"
    >
      <!-- 用户信息 -->
      <div class="user-info" @click.stop="goUserProfile(item.authorId)">
        <img class="avatar" :src="item.avatar" alt="头像" />
        <span class="username">{{ item.username }}</span>
      </div>

      <!-- 标题和内容 -->
      <div class="dynamic-header">
        <h3 class="title">{{ item.dynamicTitle }}</h3>
      </div>
      <div class="content">{{ item.dynamicContent }}</div>

      <!-- 图片区域 -->
      <div class="image-grid" v-if="item.imagesUrl?.length" @click.stop>
        <div
          class="image-wrapper"
          v-for="(img, index) in item.imagesUrl.slice(0, 3)"
          :key="index"
          @click.stop="previewImage(index, item.imagesUrl)"
        >
          <img :src="img" class="dynamic-image" />
          <div
            v-if="index === 2 && item.imagesUrl.length > 3"
            class="image-count"
            @click.stop="goDetail(item.dynamicId)"
          >
            共 {{ item.imagesUrl.length }} 张
          </div>
        </div>
      </div>

      <el-image-viewer
        v-if="showViewer"
        :url-list="imageList"
        :initial-index="startIndex"
        @close="closePreview"
      />

      <div class="footer-bar">
        <div class="topics" @click.stop>
          <span class="topic" v-for="(name, id) in item.topicMap" :key="id">
            #{{ name }}
          </span>
        </div>
        <div class="stats">
          <span class="stat">💬 {{ item.commentCount }}</span>
          <span class="stat">👍 {{ item.likeCount }}</span>
        </div>
      </div>
    </div>

    <div v-if="!list.length" class="empty-container">
      <div class="empty-text">这里空空如也，快去发布第一条动态吧！</div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

defineProps({
  list: {
    type: Array,
    required: true,
  },
})

const router = useRouter()
const showViewer = ref(false)
const imageList = ref([])
const startIndex = ref(0)
const inPreviewMode = ref(false)

const previewImage = (index, images) => {
  imageList.value = images
  startIndex.value = index
  showViewer.value = true
  inPreviewMode.value = true
}
const closePreview = () => {
  showViewer.value = false
  setTimeout(() => {
    inPreviewMode.value = false
  }, 100)
}
const goDetail = (id) => {
  router.push(`/home/dynamic/${id}`)
}
const goUserProfile = (userId) => {
  router.push(`/home/user/${userId}`)
}
</script>

<style scoped>
.dynamic-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.dynamic-card {
  padding: 16px;
  border: 1px solid #ddd;
  border-radius: 12px;
  background-color: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  cursor: pointer;
}
.dynamic-card:hover {
  background: #f8f9fa;
}
.dynamic-image {
  width: 200px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
  cursor: zoom-in; /* 鼠标变放大镜 */
  position: relative;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 10px;
}
.username {
  font-weight: bold;
  font-size: 14px;
}

.dynamic-header {
  margin-bottom: 6px;
}
.title {
  font-size: 18px;
  font-weight: bold;
}
.content {
  margin-bottom: 10px;
  color: #333;
}

.image-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 8px;
}

.image-wrapper {
  position: relative;
}
.dynamic-image {
  width: 190px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
}

/* 第3张图右上角的总数提示 */
.image-count {
  position: absolute;
  top: 6px;
  right: 6px;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 8px;
  pointer-events: auto;
  cursor: pointer;
}

.footer-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
  font-size: 13px;
  color: #777;
}
.topics {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.topic {
  background-color: #e6f0ff;
  color: #007bff;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 13px;
}
.stats {
  display: flex;
  gap: 16px;
}
.stat {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 空状态容器 */
.empty-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
  padding: 40px 20px;
  background: linear-gradient(145deg, #f8f9fa 0%, #f1f3f5 100%);
  border-radius: 16px;
  margin-top: 20px;
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.03);
}
.empty-content {
  text-align: center;
  max-width: 480px;
}

/* 文字提示 */
.empty-text {
  font-size: 18px;
  color: #666;
  margin-bottom: 32px;
  line-height: 1.6;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.8);
}
</style>
